package com.nanyang.tms.manager;

import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.Config;
import com.alipay.easysdk.kernel.util.ResponseChecker;
import com.alipay.easysdk.payment.facetoface.models.AlipayTradePrecreateResponse;
import com.nanyang.common.tool.utils.json.JsonUtil;

public class TestAlipay {
        public static void main(String[] args) throws Exception {
            // 1. 设置参数（全局只需设置一次）
            Factory.setOptions(getOptions());
            try {
                // 2. 发起API调用（以创建当面付收款二维码为例）
                AlipayTradePrecreateResponse response = Factory.Payment.FaceToFace()
                        .asyncNotify("https://www.test.com/callback")
                        .preCreate("Apple iPhone11 128G", "2234567890", "5799.00");
                // 3. 处理响应或异常
                if (ResponseChecker.success(response)) {
                    System.out.println(JsonUtil.toJson(response));
                    System.out.println("调用成功");
                } else {
                    System.err.println("调用失败，原因：" + response.msg + "，" + response.subMsg);
                }
            } catch (Exception e) {
                System.err.println("调用遭遇异常，原因：" + e.getMessage());
                throw new RuntimeException(e.getMessage(), e);
            }
        }
        private static Config getOptions() {
            Config config = new Config();
            config.protocol = "https";
            config.gatewayHost = "openapi.alipaydev.com";
            config.signType = "RSA2";
            config.appId = "2021000121699481";
            // 为避免私钥随源码泄露，推荐从文件中读取私钥字符串而不是写入源码中
            config.merchantPrivateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCM2Fs0uBeXClxj5ZsTazK+c5UAzzo7ER2haWE9RyRcO5+lbJO/GRyj9Th1St0o7u5mXTmvzm9LbxksnBy4oRdB+AZqK2N7I96HMSqGziwznf29f8GEnmXgTY08YXQv6l/cv6X51x8bvZDLu5FL6mzhCoz8L4Q9fcM73jMOsMFun2SpaRN0yDIMgKGldX1Bj+kGzcEjRrWFErGxR9IQHn3nZZaMynRHxaT0LKlyxcm9r+SCi4K2L0mpJrpF0dhz/GQd8Z1CS4XxAO0O2cYrS4RT6cg31S3CVbsbk4U7HBcogwpQ83NX2itXhbnDORHAgjAOhNleBuMs4U/PJHgbqF9vAgM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ilXHUToNLiQuAOOWyrvPqzj/q+ikt1BY7UyOSVM3Mhy9547ciPiDx+3S5KShD1HkDhslXS6/n/R4qK8n5pqhG/QB2vTfDXJLiqFsd8+VE6nvwiwrMCs68r5qL7lnvFGyL552JLCXPLjn31QL6sY=";
            //注：如果采用非证书模式，则无需赋值上面的三个证书路径，改为赋值如下的支付宝公钥字符串即可
            config.alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvl9acEGxHEH+F3in47qlCgW4swVMm9K2vuOo78rYoqSjG1crH2EM1BOF5b2vK7RZuBo0BzyJLBiJAc8KO4OHs3trGWgK0iUMgQTOTWHKFxEosLvUmGYm5HFTIUAs6uyPS/1g4X6D3ZNxWVyfhX3Nmb3FXpLzVDwK9NSywBXIge8Rpia5fKHU+cTCZYCDS8yJySUPnm98nH7HaiZOODUrZj4WDbelCwPA/4WwmF9/mn/KOOs7rinDFBl+ggXBglKwnqmFZwfsLcM44YU/MZuT23a8gcGnDHLd6RGNyeRoUya2G/wVvbSm/M36vm9ftTlc7P4x27IHqMNg7ljJvQ1nLQIDAQAB";
            //可设置异步通知接收服务地址（可选）
            config.notifyUrl = "www.baidu.com";
            return config;
        }

}
